package com.mcptt.db.model;

import java.io.Serializable;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.DatabaseUtils;

import com.cloudptt.api.comm.CheckData;
import com.cloudptt.api.product.log.Log;
import com.mcptt.db.DbConst;

public class UserInfo extends AddrInfo implements Serializable
{
	public static final int McType_Ptt = 1;
	public static final int McType_Video = 2;
	public static final int McType_Data = 3;

	private String mcpttId = "";
	private String mcvideoId = "";
	private String mcdataId  = "";


	public UserInfo(String addrId,String groupId)
	{
		super(UserType_User,"U_"+addrId,addrId,0);
		this.mcpttId = AddrInfo.toUrlFromNum(addrId);
		this.mcvideoId = AddrInfo.toUrlFromNum(addrId);
		this.mcdataId = AddrInfo.toUrlFromNum(addrId);
		this.attr = 0;
	}

	public UserInfo(int userType,String name,String addrId)
	{
		super(userType,name,addrId,0);
		this.mcpttId = "";
		this.mcvideoId = "";
		this.mcdataId  = "";
	}
	
	public UserInfo(String addrId, String name,String mcpttId,String mcvideoId,String mcdataId)
	{
		super(UserType_User,name,addrId,0);
		this.mcpttId = mcpttId;
		this.mcvideoId = mcvideoId;
		this.mcdataId = mcdataId;
		this.attr = 0;
	}

	public void updateMcxId(int mcType,String mcxIdUrl)
	{
		if(mcType == McType_Ptt)
		{
			this.mcpttId = mcxIdUrl;
		}
		else if(mcType == McType_Video)
		{
			this.mcvideoId = mcxIdUrl;
		}
		else if(mcType == McType_Data)
		{
			this.mcdataId = mcxIdUrl;
		}
	}

	public UserInfo(Cursor c)
	{
		ContentValues args = new ContentValues();
		DatabaseUtils.cursorRowToContentValues(c, args);
		createFromContentValue(args);
	}

	public UserInfo() {
		this.addrId = "";
//		this.userType = UserType_Group;
		this.name = "";
		this.mcpttId = "";
	}

	public boolean isSameOne(UserInfo oldInfo)
	{
		if(oldInfo == null)
		{
			return false;
		}
		String oldMcId = oldInfo.addrId;
		if(this.addrId.equals(oldMcId))
		{
			return true;
		}
		return false;
	}

	public boolean isSameOne(String oldNum)
	{
		if(this.mcpttId.equals(oldNum))
		{
			return true;
		}
		return false;
	}

	/**
	 * Pack the object content value to store
	 * 
	 * @return The content value representing the message
	 */
	public ContentValues getContentValues() {
		ContentValues cv = new ContentValues();
		cv.put(DbConst.COL_AddrId, addrId);
		cv.put(DbConst.COL_Type,type);
		cv.put(DbConst.COL_Name, name);
		cv.put(DbConst.COL_McpttId, mcpttId);
		cv.put(DbConst.COL_McvideoId, mcvideoId);
		cv.put(DbConst.COL_McdataId, mcdataId);
		cv.put(DbConst.COL_Attr, attr);

		return cv;
	}

	
	public ContentValues getUpdateContentValues(int mcType) {
		ContentValues cv = new ContentValues();
//		cv.put(DbConst.COL_McId, mcId);
//		cv.put(DbConst.COL_UserType,userType);
//		cv.put(DbConst.COL_Name, name);
		if(mcType == UserInfo.McType_Ptt)
		{
			cv.put(DbConst.COL_McpttId, mcpttId);
		}
		else if(mcType == UserInfo.McType_Video)
		{
			cv.put(DbConst.COL_McvideoId, mcvideoId);
		}
		else if(mcType == UserInfo.McType_Data)
		{
			cv.put(DbConst.COL_McdataId, mcdataId);
		}
		cv.put(DbConst.COL_Attr, attr);

		return cv;
	}

	public final void createFromContentValue(ContentValues args) {

		Log.debug(this.getClass(), "createFromContentValue");
		String tmp_s;

		tmp_s = args.getAsString(DbConst.COL_AddrId);
		if(tmp_s != null) {
			addrId = tmp_s;
		}
		tmp_s = args.getAsString(DbConst.COL_Type);
		if(tmp_s != null) {
			type = CheckData.parseInt(tmp_s);
		}
		tmp_s = args.getAsString(DbConst.COL_Name);
		if(tmp_s != null) {
			name = tmp_s;
		}
		tmp_s = args.getAsString(DbConst.COL_McpttId);
		if(tmp_s != null) {
			mcpttId = tmp_s;
		}
		tmp_s = args.getAsString(DbConst.COL_McvideoId);
		if(tmp_s != null) {
			mcvideoId = tmp_s;
		}
		tmp_s = args.getAsString(DbConst.COL_McdataId);
		if(tmp_s != null) {
			mcdataId = tmp_s;
		}
		tmp_s = args.getAsString(DbConst.COL_Attr);
		if(tmp_s != null) {
			int value = CheckData.parseInt(tmp_s);
			if(value != -1)
			{
				attr = value;
			}
		}
	}


	public String getMcpttId() {
		return mcpttId;
	}

	public String getMcvideoId() {
		return mcvideoId;
	}

	public String getMcdataId() {
		return mcdataId;
	}

	
//	public String getGroupId() {
//		return groupId;
//	}
	public int getOnlineState(){
		return 1;
	}
	public String getDscp()
	{
		if(name != null
				&& name.length() > 0)
		{
			return name;
		}
		if(addrId != null
				&& addrId.length() > 0)
		{
			return addrId;
		}
		else
		{
			return "";
		}
	}

	public boolean isAffiliation()
	{
		if(attr ==1)
		{
			return true;
		}
		return false;
	}

	public void setIsAffiliation(boolean isAffiliatio)
	{
		if(isAffiliatio)
		{
			attr =1;
		}
		else
		{
			attr = 0;
		}
	}

	public String toString()
	{
		StringBuffer strBuf = new StringBuffer();
		strBuf.append("type = ").append(type).append(",");
		strBuf.append("addrId = ").append(addrId).append(",");
		strBuf.append("name = ").append(name).append(",");
		strBuf.append("mcpttId = ").append(mcpttId).append(",");
		strBuf.append("mcvideoId = ").append(mcvideoId).append(",");
		strBuf.append("mcdataId = ").append(mcdataId).append(".,");
		strBuf.append("attr = ").append(attr).append(".");
		

		return strBuf.toString();
	}
}
